package com.yami.shop.multishop.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yami.shop.bean.dto.ProductDockingDTO;
import com.yami.shop.bean.model.ProductDocking;
import com.yami.shop.security.multishop.util.SecurityUtils;
import com.yami.shop.service.ProductDockingService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 商品对接Controller
 */
@RestController
@RequestMapping("/product/docking")
@Api(tags = "商品对接接口")
@Slf4j
public class ProductDockingController {

    @Autowired
    private ProductDockingService productDockingService;

    @GetMapping("/page")
    @ApiOperation(value = "分页查询商品对接列表")
    public ResponseEntity<IPage<ProductDockingDTO>> pageProductDocking(
            @ApiParam("页码") @RequestParam(defaultValue = "1") Integer pageNum,
            @ApiParam("每页数量") @RequestParam(defaultValue = "10") Integer pageSize,
            @ApiParam("店铺ID") @RequestParam(required = false) Long shopId,
            @ApiParam("商品名称") @RequestParam(required = false) String prodName,
            @ApiParam("对接状态") @RequestParam(required = false) Integer dockingStatus) {
        try {
            Page<ProductDockingDTO> page = new Page<>(pageNum, pageSize);
            IPage<ProductDockingDTO> result = productDockingService.pageProductDocking(page, shopId, prodName, dockingStatus);
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            log.error("分页查询商品对接列表失败", e);
            return ResponseEntity.badRequest().build();
        }
    }

    @GetMapping("/info/{id}")
    @ApiOperation(value = "获取商品对接详情")
    public ResponseEntity<ProductDockingDTO> getProductDocking(@PathVariable("id") Long id) {
        try {
            ProductDockingDTO productDockingDTO = productDockingService.getProductDockingById(id);
            return ResponseEntity.ok(productDockingDTO);
        } catch (Exception e) {
            log.error("获取商品对接详情失败", e);
            return ResponseEntity.badRequest().build();
        }
    }

    @PostMapping("/apply")
    @ApiOperation(value = "申请商品对接")
    public ResponseEntity<Void> applyProductDocking(@ApiParam("商品id") @RequestParam Long prodId,
                                                    @ApiParam("商品分类id") @RequestParam Long categoryId,
                                                    @ApiParam("加价金额") @RequestParam Double fareIncrease,
                                                    @ApiParam("商品名称") @RequestParam String name,
                                                    @ApiParam("id") @RequestParam Long id) {
        try {
            productDockingService.applyProductDocking(prodId, categoryId, fareIncrease,SecurityUtils.getShopUser().getShopId(),name,id);
            return ResponseEntity.ok().build();
        } catch (Exception e) {
            log.error("申请商品对接失败", e);
            return ResponseEntity.badRequest().build();
        }
    }

    @PostMapping("/audit")
    @ApiOperation(value = "审核商品对接")
    public ResponseEntity<Void> auditProductDocking(
            @ApiParam("对接ID") @RequestParam Long id,
            @ApiParam("审核状态（1-通过 2-拒绝）") @RequestParam Integer dockingStatus,
            @ApiParam("拒绝原因") @RequestParam(required = false) String rejectReason) {
        try {
            productDockingService.auditProductDocking(id, dockingStatus, rejectReason);
            return ResponseEntity.ok().build();
        } catch (Exception e) {
            log.error("审核商品对接失败", e);
            return ResponseEntity.badRequest().build();
        }
    }

    @DeleteMapping("/cancel/{id}")
    @ApiOperation(value = "取消商品对接")
    public ResponseEntity<Void> cancelProductDocking(@PathVariable("id") Long id) {
        try {
            productDockingService.cancelProductDocking(id);
            return ResponseEntity.ok().build();
        } catch (Exception e) {
            log.error("取消商品对接失败", e);
            return ResponseEntity.badRequest().build();
        }
    }
} 